package com.aim.util;

import java.util.ArrayList;

import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import com.aim.api.AIMCaseConstants;


public class WebServiceUtil {
	static Context context;	
	static boolean result = false;
	public static Context getContext() {
		return context;
	}
	public static void  setContext(Context contextIn) {
		context = contextIn;
	}
//	public static boolean isDebug()
//	{
//		SharedPreferences preferences = context.getSharedPreferences(Preferences.NAME, 0);
//		boolean isDebug=preferences.getBoolean("checkboxPref", true);
//		return isDebug;
//	}

	public static String callWebservice(String functionName, ArrayList parameterList) {
		String returnXML = "";
		
		SharedPreferences preferences = context.getSharedPreferences(Preferences.NAME, 0);
		String userName=preferences.getString(AIMCaseConstants.USERNAME, "ASHISH");
		String password=preferences.getString(AIMCaseConstants.PASSWORD, "nobel123");
		Log.e(userName, password);
//		String userName=preferences.getString(AIMCaseConstants.USERNAME, "c.wang");
//		String password=preferences.getString(AIMCaseConstants.PASSWORD, "Nobel123");
		String imei = preferences.getString(AIMCaseConstants.IMEI, "123");
		String validateCode=preferences.getString(AIMCaseConstants.VALIDATECODE, "nobel");
		String contextURL=preferences.getString(AIMCaseConstants.CONTEXTURL, AIMCaseConstants.NOBELSERVICECONTEXT);		
		SoapObject request = new SoapObject(AIMCaseConstants.NOBELNAMESPACE,functionName);
		String fullFunctionPath=contextURL+functionName;
		
		Log.e("WEbservice",functionName);		
		request.addProperty("userName",userName);
		request.addProperty("encPassword",getEncryptPassword(password,validateCode));
		request.addProperty("salt",imei);
		for(int i=0;i<parameterList.size();i++)
		{
			KeyAndValuePair pair=(KeyAndValuePair)parameterList.get(i);
		
			request.addProperty(pair.getKeyName(),pair.getKeyValue());
		}
		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE httpTransportSE = new HttpTransportSE(AIMCaseConstants.NOBELSERVICEURL);
		httpTransportSE.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
		try {
			httpTransportSE.call(fullFunctionPath,envelope);
			returnXML=envelope.getResponse().toString();
		} catch (Exception e) {
			Log.e("WebServiceUtil", "Error In Call Service", e);
			returnXML="<?xml version=\"1.0\" encoding=\"UTF-8\"?><error>"+e.getMessage()+"</error>";
			Log.e("Exception",returnXML);
		}	

		return returnXML;
	}
	
	public static Boolean login(String functionName, String s1, String s2, String imei) {
		String returnXML = "";
		SharedPreferences preferences = context.getSharedPreferences(Preferences.NAME, 0);
		String userName=s1;
		String password=s2;
		String validateCode=preferences.getString(AIMCaseConstants.VALIDATECODE, "nobel");
		String contextURL=preferences.getString(AIMCaseConstants.CONTEXTURL, AIMCaseConstants.NOBELSERVICECONTEXT);		
		SoapObject request = new SoapObject(AIMCaseConstants.NOBELNAMESPACE,functionName);
		String fullFunctionPath=contextURL+functionName;

		request.addProperty("userName",userName);
		request.addProperty("encPassword",getEncryptPassword(password,validateCode));
		request.addProperty("imei",imei);

		SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
				SoapEnvelope.VER11);
		envelope.dotNet = true;
		envelope.setOutputSoapObject(request);
		HttpTransportSE httpTransportSE = new HttpTransportSE(AIMCaseConstants.NOBELSERVICEURL);
		httpTransportSE.setXmlVersionTag("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");

		try {
			httpTransportSE.call(fullFunctionPath,envelope);

			returnXML=envelope.getResponse().toString();
			Log.e("WebServiceUtil",  returnXML);
			result = ParseXMLString.checkLoginKsoap(returnXML);
			
		} catch (Exception e) {
			Log.e("WebServiceUtil", "Error In Call Service", e);
		}	
		return result;
	}
	/**
	 * 
	 * @param password
	 * @return
	 */
	private static String getEncryptPassword(String password,String salt)
	{
		return password;
	}

	
}

